package com.timothy.android.view;

import java.util.List;

import com.timothy.android.util.NumberUtil;
import com.timothy.answer.ChoiceResult;
import com.timothy.chart.CanvasSetting;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;

public class Bar2DViewHon extends BaseView{

	private final static String LOG_TAG = "VerticalBar2D";

	protected SurfaceHolder mHolder;
	protected Canvas canvas;
	protected int canvasHeight;
	protected int canvasWidth;
	
	public Bar2DViewHon(Context context,BaseBean baseBean) {
		super(context,baseBean);
	}

	public void surfaceCreated(SurfaceHolder holder) {
		
		CanvasSetting setting = new CanvasSetting(canvasHeight,canvasWidth,Color.BLACK,1);
		
		//set canvas background color
		canvas.drawColor(setting.getBgColor());
		
		//write question
		Paint mPaint = new Paint();
		mPaint.setColor(Color.parseColor(questionTextColor));		
		mPaint.setTextSize(questionTextSize);
		int queX = setting.getLeftMargin();
		int queY = setting.getTopMargin();
//		Log.i(LOG_TAG, "VerticalBar2DView:queX:"+queX);
//		Log.i(LOG_TAG, "VerticalBar2DView:queY:"+queY);
		canvas.drawText(baseBean.getQuestionText(),queX,queY,mPaint);
		canvas.save();
		
		//get statistic data
		List<ChoiceResult> crList = baseBean.getCrList();
		
		int canvasNoMarginHeight = canvasHeight - setting.getTopMargin() - setting.getBottomMargin();
		int canvasNoMarginWidth = canvasWidth-setting.getLeftMargin()-setting.getRightMargin();
		
		double barHeightInclSpace = (canvasNoMarginHeight * 1.0)/crList.size();
		int barHeight = (int)(barHeightInclSpace*0.9);
		int spaceHeight = (int)(barHeightInclSpace*0.1);
		if(spaceHeight<1) spaceHeight = 1;	
		setting.setSpace(spaceHeight);
		
		//draw bar list
		
		for(int i=0;i<crList.size();i++){
			ChoiceResult cr = crList.get(i);
			
			//calculate data and share
			int votes = cr.getCount();
			double votesShare = cr.getShare();
			int barWidth = (int) (votesShare * canvasNoMarginWidth);
			Log.i(LOG_TAG, "VerticalBar2DView:votes:"+votes);
			Log.i(LOG_TAG, "VerticalBar2DView:votesShare:"+votesShare);
			Log.i(LOG_TAG, "VerticalBar2DView:barWidth:"+barWidth);
			
			//draw bar
			int tlX = setting.getLeftMargin();
			int tlY = setting.getTopMargin()+ (int)(i*(barHeight + spaceHeight));
			int brX = tlX + barWidth;
			int brY = tlY + barHeight;
			Log.i(LOG_TAG, "VerticalBar2DView:tlX,tlY,brX,brY="+tlX+","+tlY+","+brX+","+brY+",");
			mPaint.setColor(Color.parseColor(chartColorArr[i%8]));
			Log.i(LOG_TAG, "VerticalBar2DView:fullColor="+ chartColorArr[i%8]);
			canvas.drawRect(tlX,tlY,brX,brY, mPaint);
			canvas.save();
			
			//draw choice text
			mPaint.setTextSize(choiceTextSize);
			mPaint.setColor(Color.parseColor(choiceTextColor));
			canvas.drawText(cr.getContent(), tlX, canvasHeight-setting.getBottomMargin()/2, mPaint);
			Log.i(LOG_TAG, "VerticalBar2DView:ChoiceText="+cr.getContent());
			canvas.save();
			
			//draw votes and share
			mPaint.setTextSize(answerTextSize);
			mPaint.setColor(Color.parseColor(answerTextColor));
			canvas.drawText(String.valueOf(cr.getCount()), tlX, tlY-20, mPaint);
			canvas.drawText(NumberUtil.formatData1Deci(cr.getShare()*100)+"%", tlX, tlY-40, mPaint);
			canvas.save();
			
		}

		canvas.restore();
		mHolder.unlockCanvasAndPost(canvas);
	}
	
}
